package us.mooresr.geartrack.client.core.presenter;

import java.util.List;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;
import us.mooresr.geartrack.client.place.NameTokens;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import us.mooresr.geartrack.client.core.presenter.MainPresenter;
import us.mooresr.geartrack.client.events.ItemTypeChangedEvent;
import us.mooresr.geartrack.client.events.ItemTypeCreatedEvent;
import us.mooresr.geartrack.client.events.ItemTypeDeletedEvent;
import us.mooresr.geartrack.client.events.LocationCreatedEvent;
import us.mooresr.geartrack.shared.action.DeleteItemTypeAction;
import us.mooresr.geartrack.shared.action.DeleteItemTypeResult;
import us.mooresr.geartrack.shared.action.GetAllItemTypes;
import us.mooresr.geartrack.shared.action.GetAllItemTypesAction;
import us.mooresr.geartrack.shared.action.GetAllItemTypesResult;
import us.mooresr.geartrack.shared.action.GetAllLocationsAction;
import us.mooresr.geartrack.shared.action.GetAllLocationsResult;
import us.mooresr.geartrack.shared.action.PutItemTypeAction;
import us.mooresr.geartrack.shared.action.PutItemTypeResult;
import us.mooresr.geartrack.shared.model.ItemType;

public class ItemTypePresenter extends
		Presenter<ItemTypePresenter.MyView, ItemTypePresenter.MyProxy>
		implements ItemTypeUiHandlers {
	
	private final DispatchAsync dispatcher;

	public interface MyView extends View, HasUiHandlers<ItemTypeUiHandlers> {
		public void setItemTypeList(List<ItemType> list);
		public void addItemTypeToList(ItemType itemType);
		public void removeItemTypeFromList(ItemType itemType);
		public void updateListEntry(ItemType itemType);
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.itemtypes)
	public interface MyProxy extends ProxyPlace<ItemTypePresenter> {
	}

	@Inject
	public ItemTypePresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, final DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		getView().setUiHandlers(this);
		
		this.dispatcher = dispatcher;
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, MainPresenter.TYPE_SetMainContent, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		
		addRegisteredHandler(ItemTypeCreatedEvent.getType(), new ItemTypeCreatedEvent.ItemTypeCreatedHandler() {

			@Override
			public void onItemTypeCreated(ItemTypeCreatedEvent event) {
				getView().addItemTypeToList(event.getItemType());
				
			}
			
		});
		
		addRegisteredHandler(ItemTypeDeletedEvent.getType(), new ItemTypeDeletedEvent.ItemTypeDeletedHandler() {
			
			@Override
			public void onItemTypeDeleted(ItemTypeDeletedEvent event) {
				getView().removeItemTypeFromList(event.getItemType());
				
			}
		});
		
		addRegisteredHandler(ItemTypeChangedEvent.getType(), new ItemTypeChangedEvent.ItemTypeChangedHandler() {
			
			@Override
			public void onItemTypeChanged(ItemTypeChangedEvent event) {
				getView().updateListEntry(event.getItemType());
				
			}
		});
	}
	
	@Override
	public void onReveal() {
		super.onReveal();
		
		// Launch a request to get the item type list data
		GetAllItemTypesAction action = new GetAllItemTypesAction();
		
		dispatcher.execute(action,  new AsyncCallback<GetAllItemTypesResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}


			@Override
			public void onSuccess(GetAllItemTypesResult result) {
				getView().setItemTypeList(result.getItemTypes());
				
			}
			
		});	
	}

	@Override
	public void addItemType(ItemType itemType) {
		PutItemTypeAction action = new PutItemTypeAction(itemType);
		
		dispatcher.execute(action,  new AsyncCallback<PutItemTypeResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(PutItemTypeResult result) {
				ItemTypeCreatedEvent.fire(getEventBus(), result.getItemTypeOut());
				
			}
			
		});
		
	}

	@Override
	public void deleteItemType(ItemType itemType) {
		DeleteItemTypeAction action = new DeleteItemTypeAction(itemType);
		
		dispatcher.execute(action, new AsyncCallback<DeleteItemTypeResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(DeleteItemTypeResult result) {
				ItemTypeDeletedEvent.fire(getEventBus(), result.getItemTypeOut());
				
			}
			
		});
		
	}

	@Override
	public void updateItemType(ItemType itemType) {
		PutItemTypeAction action = new PutItemTypeAction(itemType);
		
		dispatcher.execute(action, new AsyncCallback<PutItemTypeResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(PutItemTypeResult result) {
				ItemTypeChangedEvent.fire(getEventBus(), result.getItemTypeOut());
				
			}
			
		});
		
	}
}
